﻿//页面类定义，页面名字默认是该类名
var system = {		
	//定义数据模型
	dataMode : {
			menuData : [],
			children : [],
			userName : '', // 当前用户名
			systemType : '', // 系统类型ID
			desktopMenu : [],
			menuId : null
		},
    init: function() {     	
    	this.eventBind();
    	
    	this.dataMode.systemType = getUrlParam("systemType");
    	var menuId = getUrlParam("menuid");
    	if (menuId != null && menuId > 0){
    		this.dataMode.menuId = menuId;
    	}    	

    	this.getUserInfo();
    	this.getMenuInfo(); 
    	this.showLeftMenu();
    },    
    eventBind: function() {
    	$("#frameMain").bind('load',function() {    		
    		$.unblockUI();    		
    	});
    	window.onresize=function(){  
    	     this.changeFrameHeight();  
    	} 
    },
    changeFrameHeight:function(){
    	var mainh = document.documentElement.clientHeight - 110;
    	$(".right-tb").height(mainh + "px");    	
    },
    showLeftMenu:function () {     	
    	if (this.dataMode.children.length > 0) {
    		$("#subMenu").show();
    		$(".right-tb").css("width","85%");
    	} else {
    		$("#subMenu").hide(); 
    		$(".right-tb").css("width","100%");
    	} 
    	
    	this.changeFrameHeight();
    },
    getUserInfo:function () {
        var url = "/bfs/user/getUserInfo";       
        ajaxRequest(url, null, null, null,
            function(resp) {
        	    system.dataMode.userName = resp.username;
        	    $("#userName").html(resp.username);
            },
            null);
    },
    getMenuInfo:function () {
        var url = "/bfs/menu/queryUserChildMenu"; 
        var data = {
        		systemType : this.dataMode.systemType,        		
        		menuId: this.dataMode.menuId
        	};
        ajaxRequest(url, null, data, null,
            function(resp) {         	    
        	    system.dataMode.menuData=resp.rows;
        	    system.fillFirstTemplate();
        	    system.mouseEvent();
        	    system.loadMainPageEvent(system.dataMode.menuData[0].data.menuUrl, system.dataMode
    					.menuData[0].children, 0, system.dataMode.menuData[0].data.menuId);
    			$("#navBarMenu li")[0].click();
    			if (system.dataMode.menuData[0].children != null
    					&& system.dataMode.menuData[0].children.length > 0 && $("#subMenu a").length > 0) {
    					$("#subMenu a")[0].click();
    			}                
            });
            	
    },
    
    mouseEvent:function() {
    	// 绑定菜单点击事件,主要是改变样式
    	$("#navBarMenu li").bind('click', function() {   		
    		
    		$("#navBarMenu a").removeClass('on');    		  		
    		
    		$(this).find("a").addClass("on");
    		
    		system.showLeftMenu();
    	});

    	$("#subMenu li").bind('click', function() {   
    		$("#subMenu li").removeClass('on');  
    		
    		$(this).addClass('on');
    	});
    },
    loadMainPageEvent:function(tabUrl, index, type, menuId) {    	
    	var SubMenus=null;
    	if (index > 0){
    		SubMenus = this.dataMode.menuData[index].children;
    	}
    	if (type === 0) {
    		this.dataMode.children=[];
    	}
    	if (tabUrl !== "" && tabUrl != null) {
    		if (tabUrl.indexOf("?") > -1) {
    			tabUrl += "&menuid=" + menuId;
    		} else {
    			tabUrl += "?menuid=" + menuId
    		}
    		if (tabUrl.indexOf("?") > -1) {
    			tabUrl += "&systemType=" + this.dataMode.systemType;
    		} else {
    			tabUrl += "?systemType=" + this.dataMode.systemType;
    		}
    		if (tabUrl.indexOf("?") > -1) {
    			tabUrl += "&ver=" + Math.random();
    		} else {
    			tabUrl += "?ver=" + Math.random();
    		}
    	}

    	if (!(SubMenus === null || SubMenus.length < 1)) {
    		this.dataMode.children=[];
    		this.dataMode.children=SubMenus;
    		system.fillSecondTemplate();
    		this.showLeftMenu();
    		this.mouseEvent();
    		$("#subMenu a")[0].click();
    		return;
    	}

    	$.blockUI({
    		message : "",
    		fadeIn : 300,
    		fadeOut : 300,
    		css : {
    			width : '250px',
    			top : ($(window).height() - 100) / 2 + 'px',
    			left : ($(window).width() - 200) / 2 + 'px',
    			border : 'none',
    			padding : '5px',
    			backgroundColor : '#000',
    			'-webkit-border-radius' : '5px',
    			'-moz-border-radius' : '5px',
    			opacity : .7,
    			fontSize : '16px',
    			fontWeight : 'bold',
    			color : '#fff'
    		}
    	});

    	if(tabUrl !== null && tabUrl !== undefined && tabUrl.indexOf("bfs")>-1){
    		tabUrl = "views\\" + tabUrl;
    	}
    	
    	$("#frameMain").attr("src", tabUrl);
    },
   fillFirstTemplate:function(){	   
	   var content = template('firstMenu', this.dataMode);
	   $('#navBarMenu').html(content);
   },
   fillSecondTemplate:function(){
	   var content = template('secondMenu', this.dataMode);
	   $('#subMenuItem').html(content);
   }
};